package liudan.archetype.servlet;


import liudan.archetype.annotation.RequestMapping;
import liudan.archetype.enums.Methods;
import liudan.archetype.xmlutil.ConfingBeans;
import liudan.archetype.xmlutil.XmlBean;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Title
 * Copyright: Copyright (c) 2017
 * User: liudan
 * Created on 2017/2/18 15:36
 */
public class StrutsListen implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            String xml = servletContextEvent.getServletContext().getInitParameter("mvc-config");
            Map<String, String> beanMap = ConfingBeans.readConfig(servletContextEvent.getServletContext().getRealPath(xml));
            String packagePath = beanMap.get("action-package");
            String classPath = this.getClass().getClassLoader().getResource("").getPath() + packagePath.replace(".", "/");
            File[] fileList = new File(classPath).listFiles();
            Map<String, XmlBean> map = new HashMap<String, XmlBean>();
            for (File file : fileList) {
                String actionName = file.getName().replace(".class", "");
                Class c = Class.forName(packagePath + "." + actionName);
                if (c.isAnnotationPresent(RequestMapping.class)) {
                    RequestMapping annotation = (RequestMapping) c.getAnnotation(RequestMapping.class);
                    XmlBean xmlBean = new XmlBean();
                    xmlBean.setActionType(c.getPackage().getName() + "." + actionName);
                    xmlBean.setBeanName(actionName);
                    Method[] methods = c.getMethods();
                    Map<String, Methods> methodMap = null;
                    for (Method m : methods) {
                        if(m.isAnnotationPresent(RequestMapping.class)){
                            methodMap = new HashMap<String, Methods>();
                            Methods ms = new Methods();
                            ms.setName(m.getName());
                            String url = "";
                            String mode = "";
                            if (m.isAnnotationPresent(RequestMapping.class)) {
                                RequestMapping requestMapping = m.getAnnotation(RequestMapping.class);
                                url = requestMapping.value();
                                System.out.println(requestMapping.method());
                                mode = requestMapping.method().toString();
                            }
                            xmlBean.setMode(mode);
                            xmlBean.setMethod(m.getName().replace("()",""));
                            methodMap.put(annotation.value() + url, ms);
                            xmlBean.setPath(annotation.value()+url);
                            map.put(annotation.value()+url, xmlBean);
                        }
                    }
                }
            }
            ServletContext servletContext = servletContextEvent.getServletContext();
            servletContext.setAttribute("beans", map);
            System.out.println("系统初始化完成................");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("系统初始化异常" + e);
        }
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("提示：系统已经注销");
    }
}
